{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eea2b0bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import threading\n",
    "import tensorflow\n",
    "import keras\n",
    "import tensorflow.keras.models\n",
    "from tensorflow.keras import backend as K\n",
    "import tensorflow as tf\n",
    "from tensorflow.keras.models import Sequential, model_from_json\n",
    "from tensorflow.keras.layers import Dense, Dropout, Flatten\n",
    "from tensorflow.keras.layers import Conv2D, MaxPooling2D\n",
    "from tensorflow.keras.datasets import mnist\n",
    "from tensorflow.keras.preprocessing import image\n",
    "from PIL import Image\n",
    "import PIL.ImageOps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ed3a8c20",
   "metadata": {},
   "outputs": [],
   "source": [
    "from flask import Flask, request\n",
    "from flask import render_template\n",
    "import imageio\n",
    "import numpy as np\n",
    "import re\n",
    "import sys\n",
    "import os\n",
    "import base64\n",
    "\n",
    "app = Flask(__name__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "bb823d36",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('model.json', 'r') as f:\n",
    "    loaded_model_json = f.read()\n",
    "    loaded_model = model_from_json(loaded_model_json)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8321b113",
   "metadata": {},
   "outputs": [],
   "source": [
    "loaded_model.load_weights('model.h5')\n",
    "loaded_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ed65fd08",
   "metadata": {},
   "outputs": [],
   "source": [
    "def convertImage(imgData):\n",
    "    imgstr = re.search(b'base64,(.*)', imgData).group(1)\n",
    "    with open('output.png', 'wb') as output:\n",
    "        output.write(base64.b64decode(imgstr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9248feae",
   "metadata": {},
   "outputs": [],
   "source": [
    "@app.route('/')\n",
    "@app.route('/index')\n",
    "def index():\n",
    "    return render_template('index.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "130cecea",
   "metadata": {},
   "outputs": [],
   "source": [
    "@app.route('/predict/', methods=['GET', 'POST'])\n",
    "def predict():\n",
    "    imgData = request.get_data()\n",
    "    convertImage(imgData)\n",
    "    print('debug')\n",
    "    img = image.load_img('output.png', target_size=(28,28), color_mode = 'grayscale')\n",
    "    img = PIL. ImageOps.invert(img)\n",
    "    sample = np.array(img)\n",
    "    sample = np.reshape(sample, (1, 28, 28, 1))\n",
    "    sample = sample.astype('float32')\n",
    "    sample = sample / 255\n",
    "    out = loaded_model.predict (sample)\n",
    "    print (\"debug2\")\n",
    "    print (out)\n",
    "    print (np.argmax(out, axis=1))\n",
    "    print (\"debug3\")\n",
    "    \n",
    "    response = np.array_str(np.argmax(out, axis=1))\n",
    "    return response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f2f7c22",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "\u001b[31m   WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n",
      "\u001b[2m   Use a production WSGI server instead.\u001b[0m\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5035/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [23/Jan/2023 20:15:26] \"GET / HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug\n",
      "1/1 [==============================] - 0s 84ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:15:30] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[9.3201059e-01 1.1428276e-11 1.0070351e-05 4.3536406e-08 6.1634209e-09\n",
      "  2.7219914e-05 3.2060329e-02 5.7727096e-12 3.5883427e-02 8.3746581e-06]]\n",
      "[0]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 15ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:15:35] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[5.2022106e-06 4.0624676e-07 9.6911466e-01 2.6125127e-02 2.0442321e-16\n",
      "  1.3959200e-10 2.0406446e-12 1.2396893e-11 4.7546322e-03 2.4293083e-09]]\n",
      "[2]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 15ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:15:37] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[2.7174503e-14 6.3781899e-16 7.5413906e-09 9.9999487e-01 1.1777393e-13\n",
      "  4.6881702e-08 3.7957003e-22 1.1922670e-07 1.6922473e-14 5.0051403e-06]]\n",
      "[3]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 14ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:15:40] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[3.1875302e-09 4.8050235e-09 2.3869720e-07 5.9134164e-03 1.6584538e-01\n",
      "  4.5474599e-06 5.1189738e-12 4.0587294e-04 1.2965601e-04 8.2770091e-01]]\n",
      "[9]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 15ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:15:42] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[1.1290436e-09 8.8604712e-15 1.1948684e-17 8.2895118e-01 6.1764491e-14\n",
      "  1.4281274e-03 1.3353132e-17 4.4147761e-13 2.4497970e-11 1.6962069e-01]]\n",
      "[3]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 15ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:15:47] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[4.55450264e-24 1.13084995e-17 9.58464049e-16 1.81709524e-25\n",
      "  1.00000000e+00 1.68002759e-15 5.04004041e-27 9.46734959e-14\n",
      "  8.56097619e-20 3.16767723e-10]]\n",
      "[4]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 14ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:15:50] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[6.3625973e-11 1.4692613e-14 2.6769520e-09 2.5775041e-02 1.6015545e-16\n",
      "  9.7420537e-01 1.9484218e-05 2.6060049e-15 3.4179902e-08 6.0795255e-20]]\n",
      "[5]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 17ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:15:52] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[8.0842455e-08 1.5206976e-01 4.0687528e-08 6.9201074e-04 1.4595592e-03\n",
      "  5.6558118e-09 2.1334360e-11 4.5443960e-02 5.0548377e-05 8.0028409e-01]]\n",
      "[9]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 14ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:15:55] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[3.6834126e-11 9.9999702e-01 1.1056134e-14 6.2640226e-11 1.0510353e-10\n",
      "  1.7886409e-15 2.2788171e-12 1.7409876e-06 3.5725026e-08 1.1992629e-06]]\n",
      "[1]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 16ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:15:58] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[1.0000000e+00 1.6443580e-20 5.8074293e-19 1.7929975e-23 1.3078717e-13\n",
      "  2.0822644e-22 2.3584164e-11 3.8935616e-17 1.3081929e-14 1.1000708e-16]]\n",
      "[0]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 17ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:16:00] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[1.1011792e-32 5.2573558e-19 7.1413356e-06 2.3974273e-18 6.5579042e-11\n",
      "  9.9999285e-01 6.8268307e-23 7.3875900e-16 1.7079189e-10 1.4159600e-18]]\n",
      "[5]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 15ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:16:28] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[1.04400585e-13 1.40164804e-03 9.98515189e-01 8.48558102e-06\n",
      "  1.82685803e-16 3.68405135e-17 7.12706121e-16 7.45378202e-05\n",
      "  1.44322826e-07 5.80550018e-12]]\n",
      "[2]\n",
      "debug3\n",
      "debug\n",
      "1/1 [==============================] - 0s 15ms/step\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "127.0.0.1 - - [23/Jan/2023 20:16:32] \"POST /predict/ HTTP/1.1\" 200 -\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "debug2\n",
      "[[9.3762967e-17 3.1366699e-15 4.2425044e-12 6.3464239e-10 9.2023604e-14\n",
      "  1.0000000e+00 6.2463825e-19 6.7069276e-15 2.6422970e-16 2.4572570e-08]]\n",
      "[5]\n",
      "debug3\n"
     ]
    }
   ],
   "source": [
    "port = int(os.environ.get('PORT', 5035))\n",
    "app.run(host='127.0.0.1', port=port, threaded=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7116c55c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
